09. Challenge 4

Class Basics

In this challenge, I want to flip the tables a bit. In the previous quizzes, I've been giving you header files and asking you to implement methods from them. In this quiz, I'm going to give you a .cpp file and ask you to write the corresponding header file.

In Car.cpp, you'll find the implementation of a simple Car class. This is a very unreliable car that has a 50/50 chance of being broken after every drive.

I want you to examine Car.cpp and write the corresponding header file, Car.h. The code you'll find below won't compile without a working header file. Check out the compiler errors and make it work!

Note: In the below quiz, we've added comments over the top of the function definitions in the .cpp file, although typically these comments should be placed over the function declarations in the .h file instead - we've placed them here since .h is not implemented yet.

Task List:

Task Feedback:

Great! Test and submit your code below!

Start Quiz:

// Your code goes here!
// Take a look at Car.cpp to see how to define the Car class.

// Hint: you'll need to define:
// 1. the class itself
// 2. the class constructor
// 3. one private property
// 4. three public methods
/**
 * This is how the car works. No need to make any changes here.
 */

#include "Car.h"

#include <stdlib.h>
#include <time.h>
#include <iostream>

// Constructor.
Car::Car() {
  // initialize random seed for wearAndTear
  srand(time(NULL));
  // start off in working condition
  in_working_condition_ = true;
}

// Determine whether or not the car is still drivable after some wear and tear.
void Car::wearAndTear() {
  // 50% chance that the car is still working after wear and tear
  int condition = rand() % 10;
  condition >= 5 ? in_working_condition_ = true : in_working_condition_ = false;
}

// Try to drive the car.
bool Car::drive() {
  bool didDrive = false;

  if (in_working_condition_) {
    std::cout << "Driving!" << std::endl;
    wearAndTear();
    didDrive = true;
  } else {
    std::cout << "Broken down. Please fix." << std::endl;
    didDrive = false;
  }

  return didDrive;
}


// Fix the car.
void Car::fix() {
  in_working_condition_ = true;
  std::cout << "Fixed!" << std::endl;
}
/**
 * Here is a test of your code. Feel free to play with it but there's 
 *   no need to edit this file. Remember, you're only trying to make 
 *   your code compile.
 */

#include "Car.h"

int main() {
  Car car;
    
  // try to drive 10 times
  for (int i = 0; i < 10; i++) {
    bool didDrive = car.drive();
    if (!didDrive) {
      // car is broken! must fix it
      car.fix();
    }
  }
    
  return 0;
}